const express = require('express')

const app = express()

const myLogger = (req) => {
  console.log(req.method, req.url, Date.now())
}

function json(options) {
  return (req, res, next) => {
    req.foo = 'bar'
    res.abc = () => {
      console.log('abc')
    }
    console.log(`hello ${options.message}`)
    next()
  }
}

// json中间件中定义了 req.foo, res.abc， 可以在后续的中间件中进行使用
app.use(
  json({
    message: 'world'
  })
)

function fn(req, res, next) {
  next()
}
app.use(fn)

app.get('/', (req, res, next) => {
  console.log(req.foo)
  res.abc()
  res.send('get /')
  next()
})

app.get('/about', (req, res) => {
  res.abc()
  res.send('get /about')
})

app.post('/login', (req, res) => {
  console.log(req.foo)
  res.abc()
  res.send('post /login')
})

app.listen(3000, () => {
  console.log(`Server running at http://localhost:3000/`)
})
